package com.saas.vertx.auth.jwt;


import com.saas.util.JwtUtil;
import com.saas.vertx.common.ApiHandler;
import com.saas.vertx.common.RouterVerticle;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.json.JsonObject;
import org.jose4j.jwt.MalformedClaimException;
import org.jose4j.lang.JoseException;
import org.springframework.util.CollectionUtils;

/**
 * @Author: lzqing
 * @Description: JWT Auth Verticle
 * @Date: Created in 2021/6/6
 */
public class JWTAuthVerticle extends AbstractVerticle implements RouterVerticle {

    @Override
    public void start() throws Exception {
        super.start();
        routers.addAll(CollectionUtils.arrayToList(new ApiHandler[]{
                new ApiHandler(ApiHandler.get(), "/getToken", ctx -> {
                    ctx.response().putHeader("Content-Type", "text/plain");
                    String jwt = null;
                    try {
                        jwt = JwtUtil.generateToken();
                    } catch (JoseException e) {
                        e.printStackTrace();
                    } catch (MalformedClaimException e) {
                        e.printStackTrace();
                    }
                    ctx.response().end(jwt);
                })}
        ));
    }

}
